//
//  WLComposeBottomView.swift
//  WLWeibo
//
//  Created by Devil on 16/8/17.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit
enum WLComposeBottomViewType: Int {
    //图片
    case Picture = 0
    //@
    case Mention = 1
    //#
    case Trend = 2
    //笑脸
    case Emoticon = 3
    //+
    case Add = 4
}


class WLComposeBottomView: UIView {
    //声明闭包
    var closure:((type: WLComposeBottomViewType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听事件
    func buttonClick(button: UIButton){
        //执行闭包
        closure?(type: WLComposeBottomViewType(rawValue: button.tag)!)
    }
    
    //设置视图
    private func setupUI(){
    
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        //添加按键
        let pictureButton = addChildButtons("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background", type: .Trend)
        let emoticonButton = addChildButtons("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButtons("compose_add_background", type: .Add)
        
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        emoticonButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)



        }
    
        
    
    }
    
    func addChildButtons(imgName: String,type: WLComposeBottomViewType) -> UIButton{
    
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }

}
